package com.qiantu.phone.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;

import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.widget.layout.SettingBar;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.LinkageNewBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.adapter.ListLabelAdapter;
import com.qiantu.phone.ui.dialog.BottomDateDialog;
import com.qiantu.phone.ui.dialog.BottomListDialog;
import com.qiantu.phone.ui.dialog.BottomNormalTimeDialog;
import com.qiantu.phone.ui.dialog.BottomTimePeriodDialog;
import com.qiantu.phone.utils.LinkageDataHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class EditTimeActivityNew extends AppActivity {
    private SettingBar settingAction;
    private SettingBar settingTime;
    private SettingBar beginTime;
    private SettingBar endTime;
    private CheckBox onlyBox;
    private CheckBox everydayBox;
    private CheckBox weekdayBox;
    private CheckBox weekendBox;
    private CheckBox weekBox;
    private CheckBox somedayBox;
    private View selectDayLayout;
    private View btnMonday;
    private View btnTuesday;
    private View btnWednesday;
    private View btnThursday;
    private View btnFriday;
    private View btnSaturday;
    private View btnSunday;
    private String serialNo;
    private LinkageNewBean mLinkageBean;
    private LinkageNewBean.LinkageTimeCondition mLinkageCondition;
    private BottomListDialog mBottomActionDialog;
    private BottomTimePeriodDialog mBottomTimePeriodDialog;
    private BottomNormalTimeDialog mBottomNormalTimeDialog;
    private BottomDateDialog mBottomDateDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_time_new;
    }

    @Override
    protected void initView() {
        mLinkageCondition =  getSerializable("condition");
        settingAction = findViewById(R.id.setting_linkage_action);
        settingTime = findViewById(R.id.setting_linkage_time);
        onlyBox = findViewById(R.id.linkage_time_only_check);
        everydayBox = findViewById(R.id.linkage_time_everyday_check);
        weekdayBox = findViewById(R.id.linkage_time_weekday_check);
        weekendBox = findViewById(R.id.linkage_time_weekend_check);
        weekBox = findViewById(R.id.linkage_time_week_check);
        somedayBox = findViewById(R.id.linkage_time_someday_check);
        selectDayLayout = findViewById(R.id.select_day_layout);
        btnMonday = findViewById(R.id.btn_monday);
        btnTuesday = findViewById(R.id.btn_tuesday);
        btnWednesday = findViewById(R.id.btn_wednesday);
        btnThursday = findViewById(R.id.btn_thursday);
        btnFriday = findViewById(R.id.btn_friday);
        btnSaturday = findViewById(R.id.btn_saturday);
        btnSunday = findViewById(R.id.btn_sunday);
        beginTime = findViewById(R.id.linkage_time_begintime);
        endTime = findViewById(R.id.linkage_time_endtime);
        setOnClickListener(settingAction, settingTime, btnMonday, btnTuesday, btnWednesday, btnThursday, btnFriday, btnSaturday, btnSunday, onlyBox, everydayBox, weekdayBox, weekendBox, weekBox, somedayBox, beginTime, endTime);
//        btnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if(isChecked){
//                    selectDayLayout.setVisibility(View.VISIBLE);
//                }else{
//                    selectDayLayout.setVisibility(View.GONE);
//                }
//            }
//        });
    }

    @Override
    protected void initData() {
        mLinkageBean = LinkageDataHolder.getLinkageDataNew();
        if (mLinkageCondition == null) {
            mLinkageCondition = new LinkageNewBean.LinkageTimeCondition();
            mLinkageCondition.setRepeatTypeId(1);
            mLinkageCondition.setConditionTypeId(1);
            mLinkageCondition.setActionTypeId(1);
            mLinkageCondition.setOperatorTypeId(1);
            mLinkageCondition.setFromHour(0);
            mLinkageCondition.setFromMinute(0);
            mLinkageCondition.setToHour(0);
            mLinkageCondition.setToMinute(9);
            mLinkageCondition.setIcon("icon_intelligent_condition_timing");
            List<Integer> weekDays = new ArrayList<>();
            for(int i = 0;i < 7;i++){
                weekDays.add(i);
            }
            mLinkageCondition.setRepeatWeekDays(weekDays);
            mLinkageCondition.setSort(mLinkageBean.getConditions().indexOf(mLinkageCondition) + 1);
        }
        updateView();
    }

    private void updateView() {
        setActionType();
        setTimeType(mLinkageCondition.getRepeatTypeId());
        btnMonday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(1));
        btnTuesday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(2));
        btnWednesday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(3));
        btnThursday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(4));
        btnFriday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(5));
        btnSaturday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(6));
        btnSunday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(0));
        if(mLinkageCondition.getRepeatFromTime() == null){
            beginTime.setRightText("");
        }else{
            beginTime.setRightText(mLinkageCondition.getRepeatFromTime() + " - " + mLinkageCondition.getRepeatToTime());
        }

    }

    private void setActionType() {
        if(mLinkageCondition.getActionTypeId() == 1){
            settingAction.setRightText( R.string.linkage_action_become);
            settingTime.setRightText(String.format("%02d:%02d",mLinkageCondition.getFromHour(),mLinkageCondition.getFromMinute()));
        }else{
            settingAction.setRightText( R.string.linkage_action_keep);
            settingTime.setRightText(String.format("%02d:%02d - %02d:%02d",mLinkageCondition.getFromHour(),mLinkageCondition.getFromMinute(),mLinkageCondition.getToHour(),mLinkageCondition.getToMinute()));
        }
    }


    @Override
    public void onRightClick(View view) {
//        if(btnSwitch.isChecked()
//                && !btnMonday.isSelected()
//                && !btnTuesday.isSelected()
//                && !btnWednesday.isSelected()
//                && !btnThursday.isSelected()
//                && !btnFriday.isSelected()
//                && !btnSaturday.isSelected()
//                && !btnSunday.isSelected()){
//            toast(R.string.please_select_repeat_day_hint);
//            return;
//        }
        //当前联动空，才去
        mLinkageCondition.getRepeatWeekDays().clear();
        if(btnSunday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(0);
        }
        if(btnMonday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(1);
        }
        if(btnTuesday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(2);
        }
        if(btnWednesday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(3);
        }
        if(btnThursday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(4);
        }
        if(btnFriday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(5);
        }
        if(btnSaturday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(6);
        }
        if(getBoolean("isAdd")){
            mLinkageBean.getConditions().add(mLinkageCondition);
        }else{
            int position = getInt("position");
            mLinkageBean.getConditions().remove(position);
            mLinkageBean.getConditions().add(position,mLinkageCondition);
        }
        startActivity_SingleTask(EditLinkageActivityNew.class);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_linkage_action:
                showBottomListDialog();
                break;
            case R.id.setting_linkage_time:
                if (mLinkageCondition.getActionTypeId() == 1) {
                    showBottomTimeDialog();
                } else {
                    showBottomTimePeriodDialog();
                }
                break;
            case R.id.linkage_time_only_check:
                setTimeType(1);
                break;
            case R.id.linkage_time_everyday_check:
                setTimeType(2);
                break;
            case R.id.linkage_time_weekday_check:
                setTimeType(3);
                break;
            case R.id.linkage_time_weekend_check:
                setTimeType(4);
                break;
            case R.id.linkage_time_week_check:
                setTimeType(5);
                break;
            case R.id.linkage_time_someday_check:
                setTimeType(6);
                break;
            case R.id.linkage_time_begintime:
                showBottomDateDialog();
                break;
            default:
                view.setSelected(!view.isSelected());
                break;
        }
    }

    private void setTimeType(int timeType) {
        mLinkageCondition.setRepeatTypeId(timeType);
        beginTime.setVisibility(timeType == 6 ? View.VISIBLE : View.GONE);
        endTime.setVisibility(timeType == 6 ? View.VISIBLE : View.GONE);
        selectDayLayout.setVisibility(timeType == 5 ? View.VISIBLE : View.GONE);
        onlyBox.setChecked(timeType == 1 ? true : false);
        everydayBox.setChecked(timeType == 2 ? true : false);
        weekdayBox.setChecked(timeType == 3 ? true : false);
        weekendBox.setChecked(timeType == 4 ? true : false);
        weekBox.setChecked(timeType == 5 ? true : false);
        somedayBox.setChecked(timeType == 6 ? true : false);
    }

    private void showBottomListDialog() {
        if (mBottomActionDialog == null) {
            mBottomActionDialog = new BottomListDialog(this);
            ListLabelAdapter listLabelAdapter = new ListLabelAdapter(this);
            List<String> data = new ArrayList<>();
            data.add(getString(R.string.linkage_action_become));
            data.add(getString(R.string.linkage_action_keep));
            listLabelAdapter.setData(data);
            listLabelAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    mBottomActionDialog.dismiss();
                    mLinkageCondition.setActionTypeId(position + 1);
                    setActionType();

                }
            });
            mBottomActionDialog.setAdapter(listLabelAdapter);
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomActionDialog);
        }
        ListLabelAdapter listLabelAdapter = (ListLabelAdapter) mBottomActionDialog.getAdapter();
        listLabelAdapter.setSelected("");
        mBottomActionDialog.show();
    }

    private void showBottomTimePeriodDialog() {
        if (mBottomTimePeriodDialog == null) {
            mBottomTimePeriodDialog = new BottomTimePeriodDialog(this);
            mBottomTimePeriodDialog.setDialogClickListener(new BottomTimePeriodDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(boolean isOpen, String startHour, String startMinute, String endHour, String endMinute) {
                    mLinkageCondition.setFromHour(Integer.parseInt(startHour));
                    mLinkageCondition.setFromMinute(Integer.parseInt(startMinute));
                    mLinkageCondition.setToHour(Integer.parseInt(endHour));
                    mLinkageCondition.setToMinute(Integer.parseInt(endMinute));
                    setActionType();
                    mBottomTimePeriodDialog.dismiss();
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomTimePeriodDialog);
        }
        String start = mLinkageCondition.getFromHour() + ":" + mLinkageCondition.getFromMinute();
        String end = mLinkageCondition.getToHour() + ":" + mLinkageCondition.getToMinute();

        mBottomTimePeriodDialog.setSelectedTime(true, start, end);
        mBottomTimePeriodDialog.setShowType("2");
        mBottomTimePeriodDialog.show();
    }

    private void showBottomTimeDialog() {
        if (mBottomNormalTimeDialog == null) {
            mBottomNormalTimeDialog = new BottomNormalTimeDialog(this);
            mBottomNormalTimeDialog.setDialogClickListener(new BottomNormalTimeDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(String hour, String minute) {
                    mLinkageCondition.setFromHour(Integer.parseInt(hour));
                    mLinkageCondition.setFromMinute(Integer.parseInt(minute));
                    setActionType();
                    mBottomNormalTimeDialog.dismiss();
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomNormalTimeDialog);
        }
        mBottomNormalTimeDialog.setSelectedTime(mLinkageCondition.getFromHour(),mLinkageCondition.getFromMinute());
        mBottomNormalTimeDialog.show();
    }

    private void showBottomDateDialog() {
        if (mBottomDateDialog == null) {
            mBottomDateDialog = new BottomDateDialog(this);
            mBottomDateDialog.setDialogClickListener(new BottomDateDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(String startTime, String endTime) {
                    beginTime.setRightText(startTime + " - " + endTime);
                    mLinkageCondition.setRepeatFromTime(startTime);
                    mLinkageCondition.setRepeatToTime(endTime);
                    mBottomDateDialog.dismiss();
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomDateDialog);
        }
        mBottomDateDialog.setSelectDate(mLinkageCondition.getRepeatFromTime(), mLinkageCondition.getRepeatToTime());
        mBottomDateDialog.show();
    }
}

